package com.ib.algokit.charting;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class HideSeriesDemo3 extends ApplicationFrame {
	public HideSeriesDemo3(String frameTitle, XYZDataset localXYZDataset) {
		super(frameTitle);
		setContentPane(new MyChartPanel(frameTitle, localXYZDataset));
	}

	public static void showChart(String frameTitle, DefaultXYZDataset localXYZDataset) {
		HideSeriesDemo3 localHideSeriesDemo3 = new HideSeriesDemo3(frameTitle, localXYZDataset);
		localHideSeriesDemo3.pack();
		RefineryUtilities.centerFrameOnScreen(localHideSeriesDemo3);
		localHideSeriesDemo3.setVisible(true);
	}

	class MyChartPanel extends DemoPanel implements ActionListener {
		private XYItemRenderer renderer;

		public MyChartPanel(String chartTitle, XYZDataset localXYZDataset) {
			super(new BorderLayout());
			JFreeChart localJFreeChart = createChart(chartTitle, localXYZDataset);
			addChart(localJFreeChart);
			ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
			localChartPanel.setMouseWheelEnabled(true);
			JPanel localJPanel = new JPanel();
			JCheckBox localJCheckBox1 = new JCheckBox("Series 1");
			localJCheckBox1.setActionCommand("S1");
			localJCheckBox1.addActionListener(this);
			localJCheckBox1.setSelected(true);
			JCheckBox localJCheckBox2 = new JCheckBox("Series 2");
			localJCheckBox2.setActionCommand("S2");
			localJCheckBox2.addActionListener(this);
			localJCheckBox2.setSelected(true);
			JCheckBox localJCheckBox3 = new JCheckBox("Series 3");
			localJCheckBox3.setActionCommand("S3");
			localJCheckBox3.addActionListener(this);
			localJCheckBox3.setSelected(true);
			localJPanel.add(localJCheckBox1);
			localJPanel.add(localJCheckBox2);
			localJPanel.add(localJCheckBox3);
			add(localChartPanel);
			add(localJPanel, "South");
			Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
			localChartPanel.setPreferredSize(dim);
		}

		private JFreeChart createChart(String chartTitle, XYZDataset paramXYZDataset) {
			JFreeChart localJFreeChart = ChartFactory.createBubbleChart(chartTitle, "X", "Y", paramXYZDataset, PlotOrientation.VERTICAL, true, true, false);
			XYPlot localXYPlot = (XYPlot) localJFreeChart.getPlot();
			localXYPlot.setDomainPannable(true);
			localXYPlot.setRangePannable(true);
			this.renderer = localXYPlot.getRenderer(0);
			return localJFreeChart;
		}

		public void actionPerformed(ActionEvent paramActionEvent) {
			int i = -1;
			if (paramActionEvent.getActionCommand().equals("S1")) {
				i = 0;
			} else if (paramActionEvent.getActionCommand().equals("S2")) {
				i = 1;
			} else if (paramActionEvent.getActionCommand().equals("S3")) {
				i = 2;
			}
			if (i < 0)
				return;
			boolean bool = this.renderer.getItemVisible(i, 0);
			this.renderer.setSeriesVisible(i, new Boolean(!(bool)));
		}
	}
}